﻿using DataTurnClient.Interface;
using DataTurnClient.Model;
using ReactiveUI;
using Splat;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataTurnClient.ViewModel
{
    public class TimerViewModel : ReactiveObject
    {
        public ViewModelActivator Activator { get; }

        public IScheduler MainScheduler;

        #region 属性
        public Job Job
        {
            get => _Job;
            set => this.RaiseAndSetIfChanged(ref _Job, value);
        }
        private Job _Job = new Job();

        public List<Job> JobList
        {
            get => _JobList;
            set => this.RaiseAndSetIfChanged(ref _JobList, value);
        }
        private List<Job> _JobList = new List<Job>();

        public int Indexs
        {
            get => _Indexs;
            set => this.RaiseAndSetIfChanged(ref _Indexs, value);
        }
        private int _Indexs ;

        public string ErrorMessage
        {
            get => _ErrorMessage;
            set => this.RaiseAndSetIfChanged(ref _ErrorMessage, value);
        }
        private string _ErrorMessage;

        #endregion


        #region Command

        public ReactiveCommand<Unit, Result<List<Job>>> SelectJobCommand { get; }
        public ReactiveCommand<Unit, Result<int>> SaveJobCommand { get; }

        #endregion

        public TimerViewModel()
        {
            Activator = new ViewModelActivator();
            SelectJobCommand = ReactiveCommand.Create(SelectJobCommandImpl);
            SaveJobCommand = ReactiveCommand.Create(SaveJobCommandImpl);
            this.WhenAnyValue(vm => vm.Indexs).Do(x =>
            {
                if (x > -1 && JobList.Count > 0)
                {
                    Job = JobList[x];
                }
            }).Subscribe();
        }

        private Result<List<Job>> SelectJobCommandImpl()
        {
            Result<List<Job>> result = new Result<List<Job>>()
            {
                Code = 200,
                Message = "请求成功",
                Success = true
            };
            try
            {
                var service = Locator.Current.GetService<IDBHelperService>();
                using (var db = service.SqlSugarClient(Globals.DbConnect, Globals.DbType))
                {
                    var Jobs = db.Queryable<Job>().ToList();
                    JobList = Jobs;
                    Job = Jobs.FirstOrDefault();
                    result.Data = Jobs;
                }
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = $"SelectJobCommandImpl 发生异常：{ex.Message}";
                result.Success = false;
            }
            return result;
        }

        private Result<int> SaveJobCommandImpl()
        {
            Result<int> result = new Result<int>()
            {
                Code = 200,
                Message = "请求成功",
                Success = true
            };
            try
            {
                var service = Locator.Current.GetService<IDBHelperService>();
                using (var db = service.SqlSugarClient(Globals.DbConnect, Globals.DbType))
                {
                    int change = db.Updateable<Job>()
                                        .SetColumns(it => new Job() { JobRunTime = Job.JobRunTime })
                                        .Where(it => it.JobCode == Job.JobCode)
                                        .ExecuteCommand();
                    result.Data = change;
                }
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = $"SaveJobCommandImpl 发生异常：{ex.Message}";
                result.Success = false;
            }
            return result;
        }

    }
}
